//
//  NImagePickerControl.m
//  News
//
//  Created by zhangtao on 2018/11/13.
//  Copyright © 2018 zt.td. All rights reserved.
//

#import "NImagePickerControl.h"

@implementation NImagePickerControl


-(instancetype)initWithController:(UIViewController *)controller{
    self = [super init];
    self.controller = controller;
    return self;
}

-(void)camera{
    [self presentViewToImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera];
}

-(void)album{
    [self presentViewToImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

-(void)presentViewToImagePickerWithSourceType:(UIImagePickerControllerSourceType)type{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.sourceType = type;
    picker.allowsEditing = YES;
    picker.videoQuality = .5;
    picker.delegate = self;
    [self.controller presentViewController:picker animated:YES completion:nil];
}


#pragma mark ----相机相册----
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    //    }else{
    //        image = info[UIImagePickerControllerOriginalImage];
    //    }
    //    data = UIImageJPEGRepresentation(image, _quality);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage *image;
        //    if (self.allowsEditing) {
        image = info[UIImagePickerControllerEditedImage];
        if (self.imagePickerCallBack) {
            self.imagePickerCallBack(image);
        }
    });
    
    [self.controller dismissViewControllerAnimated:YES completion:nil];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
